package cn.kgc.tangcco.io.inputstreamandoutputstram;

import cn.hutool.core.date.DateUtil;
import cn.hutool.core.date.TimeInterval;
import org.junit.Test;

import java.io.*;

/**
 * @author 李昊哲
 * @Description
 * @create 2020/9/9 下午2:03
 */
public class InputStreamAndOutPutstreamTest03 {
    @Test
    public void test01() {
        try {
            // 目标文件
            InputStream inputStream = new FileInputStream("pom.xml");
            OutputStream outputStream = new FileOutputStream("pom.txt");
            int b = -1;
            while ((b = inputStream.read()) != -1) {
                outputStream.write(b);
                outputStream.flush();
            }
            System.out.println("写入完成");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test03() {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            // 目标文件
            inputStream = new FileInputStream("pom.xml");
            outputStream = new FileOutputStream("pom.txt");
            byte[] bytes = new byte[1024 * 8];
            int b = -1;
            while ((b = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, b);
                outputStream.flush();
            }
            System.out.println("写入完成");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }

    @Test
    public void test04() {
        try {
            // 目标文件
            InputStream inputStream = new FileInputStream("/home/kgc/1.mp4");
            OutputStream outputStream = new FileOutputStream("/home/kgc/2.mp4");
            int b = -1;
            TimeInterval timer = DateUtil.timer();
            while ((b = inputStream.read()) != -1) {
                outputStream.write(b);
                outputStream.flush();
            }
            //花费毫秒数
            long interval = timer.interval();
            timer.intervalRestart();
            System.out.println("写入完成>>>" + interval + "ms");
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    @Test
    public void test05() {
        InputStream inputStream = null;
        OutputStream outputStream = null;
        try {
            // 目标文件
            inputStream = new FileInputStream("/home/kgc/1.mp4");
            outputStream = new FileOutputStream("/home/kgc/2.mp4");
            byte[] bytes = new byte[1024 * 8];
            int b = -1;
            System.out.println("正在拷贝中，请稍候...");
            TimeInterval timer = DateUtil.timer();
            while ((b = inputStream.read(bytes)) != -1) {
                outputStream.write(bytes, 0, b);
                outputStream.flush();
            }
            long interval = timer.interval();
            System.out.println("写入完成>>>" + interval + "ms");
        } catch (IOException e) {
            e.printStackTrace();
        } finally {
            try {
                if (outputStream != null) {
                    outputStream.close();
                }
                if (inputStream != null) {
                    inputStream.close();
                }
            } catch (IOException e) {
                e.printStackTrace();
            }
        }
    }
}
